#!/usr/bin/env python
# --*-- coding:utf-8 --*--
# author:g-y-b time:2021/4/13

import cv2 as cv

cap = cv.VideoCapture("./image/DOG.mp4")

_, frame = cap.read()
height = frame.shape[0]
width = frame.shape[1]

fps = cap.get(cv.CAP_PROP_FPS)  # 返回视频的fps--帧率
size = cap.get(cv.CAP_PROP_FRAME_WIDTH)  # 返回视频的宽，等同于frame.shape[1]
size1 = cap.get(cv.CAP_PROP_FRAME_HEIGHT)  # 返回视频的高，等同于frame.shape[0]

video = cv.VideoWriter('2.mp4', cv.VideoWriter_fourcc('m', 'p', '4', 'v'),
                       fps, (width, height))    # 创建视频流对象
"""
参数1 即将保存的文件路径
参数2 VideoWriter_fourcc 为视频编解码器
    fourcc意为四字符代码(Four-Character Codes),顾名思义，该编码由四个字符组成，下面是VideoWriter_fourcc对象一些常用的参数，注意：字符顺序不能弄混
    cv.VideoWriter_fourcc('I','4','2','0'),该参数是YUV编码类型，文件名后缀为.avi
    cv.VideoWriter_fourcc('P','I','M','I'),该参数是MPEG_1编码类型，文件名后缀为.avi
    cv.VideoWriter_fourcc('X','V','I','D'),该参数是MPEG-4编码类型，文件名后缀为.avi
    cv.VideoWriter_fourcc('T','H','E','O'),该参数是Ogg Voribs,文件名后缀为.ovg
    cv.VideoWriter_fourcc('F','L','V','1'),该参数是Flash视频，文件名后缀为.flv
    cv.VideoWriter_fourcc('m','p','4','v'),文件后缀名为.mp4
参数3 帧播放速度
参数4 (width,height)为视频帧大小
"""

while 1:
    ret, frame = cap.read()
    if ret:
        cv.imshow("capture", frame)
        video.write(frame)  # 向视频文件写入一帧--只有图像，没有声音
    else:
        break

    if cv.waitKey(40) and 0xFF == ord('q'):     # 等候40ms，播放下一帧，或者按q键退出
        break
cap.release()   # 释放视频流
cv.destroyAllWindows()      # 关闭所有窗口